/* 
 *  <copyright file="BaseCamera.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SobrietyEngine.Components.Graphic;

namespace SobrietyEngine.Components.Camera
{
    public abstract class BaseCamera: BaseComponent, ICamera
    {
        //public bool Active { get; set; }
        public Viewport Viewport { get; set; }                       

        protected IPosition positionComp;
        protected IOrientation orientationCmp;

        public Vector3 Position { get; set; }

        public BoundingSphere boundingSphere;
        public BoundingSphere BoundingSphere { get { return boundingSphere; } protected set { boundingSphere = value; } }

        public BoundingFrustum BoundingFrustum { get; protected set; }

        public Matrix ViewportScale { get; set; }

        //TODO: not magic numbers.
        //screen width, height
        protected Rectangle Lens = new Rectangle(0, 0, 1280, 760);

        public Matrix ProjectionMatrix { get; set; }

        public BaseCamera()
            : base()
        {
            Position = Vector3.Zero;
        }

        public override Boolean InitComponent()
        {
            base.InitComponent();

            positionComp = Actor.Get<IPosition>();
            orientationCmp = Actor.Get<IOrientation>();

            if (Viewport.Height > Viewport.Width)
                BoundingSphere = new BoundingSphere(positionComp.PositionV3, Viewport.Height);
            else
                BoundingSphere = new BoundingSphere(positionComp.PositionV3, Viewport.Width);

            //set the camera lens to be the viewport size
            Lens = new Rectangle(0, 0, Viewport.Width, Viewport.Height);

            Active = true;

            Vector2 pos = positionComp.PositionV2;
            Lens.X += (int)(pos.X - Position.X);
            Lens.X -= (int)(Lens.Width / 2);

            Lens.Y += (int)(pos.Y - Position.Y);
            Lens.Y -= (int)(Lens.Height / 2);

            if (positionComp == null)
                Logging.Logger.GetSingleton().WriteError("Camera2D::Initilize()::Actor Requires a Position Component");

            return true;
        }

        public override void Update(GameTime ElapsedTime)
        {
            base.Update(ElapsedTime);
            Vector3 pos = positionComp.PositionV3;
            if (pos != Position)
            {
                Lens.X = (int)(pos.X - (Lens.Width/2));
                Lens.Y = (int)(pos.Y - (Lens.Height/2));
                Position = pos;
                boundingSphere.Center = Position;
            }
        }

        public abstract Matrix GetViewMatrix();

        public virtual BoundingFrustum GetBoundingFrustum()
        {
            throw new NotImplementedException();
        }


    }
}